﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BadHabit.Factory.Controls {

    /// <summary>
    /// Interaction logic for EffortBar.xaml
    /// </summary>
    public partial class EffortBar : UserControl {

        const double DAY_WORK_HOURS = 6;

        internal static readonly DependencyProperty ActualEffortProperty =
            DependencyProperty.Register("ActualEffort", typeof(double), typeof(EffortBar), null);

        internal static readonly DependencyProperty ActualBarHeightProperty =
            DependencyProperty.Register("ActualBarHeight", typeof(double), typeof(EffortBar), null);

        internal static readonly DependencyProperty TodayEffortProperty =
            DependencyProperty.Register("TodayEffort", typeof(double), typeof(EffortBar), null);

        internal static readonly DependencyProperty LastDayBarHeightProperty =
            DependencyProperty.Register("LastDayBarHeight", typeof(double), typeof(EffortBar), null);

        internal static readonly DependencyProperty BarBrushProperty =
            DependencyProperty.Register("BarBrush", typeof(Brush), typeof(EffortBar), null);

        internal static readonly DependencyProperty TodayBorderBrushProperty =
            DependencyProperty.Register("TodayBorderBrush", typeof(Brush), typeof(EffortBar), null);


        public EffortBar() {
            InitializeComponent();           
        }

        public double ActualEffort {
            get {
                return (double)GetValue(EffortBar.ActualEffortProperty);
            }
            set {
                SetLastDayEffort(value);
                UpdateBar(value);         
            }
        }


        private void UpdateBar(double value) {
            SetValue(EffortBar.ActualEffortProperty, value);
            SetValue(EffortBar.ActualBarHeightProperty, CalcBarHeight(value));
            if (value > Properties.Settings.Default.LastEffortHours) {
                SetValue(EffortBar.TodayEffortProperty, Math.Round( value - Properties.Settings.Default.LastEffortHours, 1));
                SetValue(EffortBar.LastDayBarHeightProperty, CalcBarHeight(value - Properties.Settings.Default.LastEffortHours));
            } else {
                SetValue(EffortBar.TodayEffortProperty, 0.0);
                SetValue(EffortBar.LastDayBarHeightProperty, 0.0);
            }

            if (value < ThisMonthUntilNowHours) {
                SetValue(EffortBar.BarBrushProperty, Resources["UnderWorkedBrush"]);
                SetValue(EffortBar.TodayBorderBrushProperty, Resources["UnderWorkedBorderBrush"]);
            } else {
                SetValue(EffortBar.BarBrushProperty, Resources["OverWorkedBrush"]);
                SetValue(EffortBar.TodayBorderBrushProperty, Resources["OverWorkedBorderBrush"]);
            }
        }

        public double AcualBarHeight {
            get {
                return (double) GetValue(EffortBar.ActualBarHeightProperty);
            }
        }


        private double ThisMonthWorkHours {
            get {
                DateTime thisMonthFirstDay = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
                DateTime thisMonthLastDay = new DateTime(DateTime.Today.AddMonths(1).Year, DateTime.Today.AddMonths(1).Month, 1);
                return Data.ProjectDataSet.CalcWorkDayCount(thisMonthFirstDay, thisMonthLastDay) * DAY_WORK_HOURS;
            }
        }

        private double ThisMonthUntilNowHours {
            get {
                DateTime thisMonthFirstDay = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
                return Data.ProjectDataSet.CalcWorkDayCount(thisMonthFirstDay, DateTime.Today) * DAY_WORK_HOURS;
            }
        }



        

        private double CalcBarHeight(double effort) {
            double h = (ruleBorder.ActualHeight * effort) / ThisMonthWorkHours;
            if (h > ruleBorder.ActualHeight)
                h = ruleBorder.ActualHeight;

            return h;
        }

        public void Refresh() {

            if (Client.User == null)
                return;

            if (!Properties.Settings.Default.ShowEffortBar) {
                this.Visibility = Visibility.Collapsed;
                return;
            }
            else
                this.Visibility = Visibility.Visible;

            double effort = 0;
            try {
                effort = Client.Service.RetriveUserActualMonthEffort(Client.User.Identity.Name, DateTime.Today.Month, DateTime.Today.Year);
            } catch (Exception ex) {
                Windows.Error.ShowError(ex, Properties.Resources.Sorry_could_not_read_your_month_worked_hours);
                ActualEffort = 0;
                return;
            }

            ActualEffort = effort;

        }

        private void SetLastDayEffort(double value) {            
            if (!System.DateTime.Today.Equals(Properties.Settings.Default.LastEffortDate.Date)) {
                Properties.Settings.Default.LastEffortDate = System.DateTime.Today;
                Properties.Settings.Default.LastEffortHours = value;
            }
        }

        private void effortBar_SizeChanged(object sender, SizeChangedEventArgs e) {
            UpdateBar(ActualEffort);
        }



    }
}
